<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\jooj_member;

use think\Model;

class Tags extends Model {

    protected $base_table = 'jj_tags';//标签基础表
    protected $table = 'jj_tags';//标签表

    protected $connection = ['database' => 'jooj_member'];

    
    public function __construct($company_id=[]){

        if(!is_array($company_id)){
            //标签表
            $this->table = $this->base_table."_".$company_id;
        }
        
        parent::__construct($company_id);
    }


    /**
     * 分页获取tags
     * @param type $page
     * @param type $pageSize
     * @param type $where
     * @return type
     */
    public function getTagsPage($page=1,$pageSize=2,$where=[]){
        
                
        $page = $page<1?1:intval($page);
        $pageSize = $pageSize>100?100:($pageSize<2?2: intval($pageSize));
        
        //修正分页参数
        $count     = $this->getPageCount($where);
        $pageList  = $this->getPageCount($where, 0, $page, $pageSize,'name as tagName,tag_id as tagId');
        
        $data = [
            'totalCount'=>$count,
            'totalPage'=> ceil($count/$pageSize),
            'tagsList'=>$pageList,
        ];
        
        return $data;
    }
    
    /**
     * 获取分页数据和count
     * @param type $where
     * @param type $is_count
     * @param type $page
     * @param type $pageSize
     * @param type $field
     * @return type
     */
    private function getPageCount($where,$is_count=1,$page=1,$pageSize=2,$field="*"){
        if($is_count){
            return $this->where($where)->count();
        }
        
        return $this->where($where)->page($page,$pageSize)->field($field)->select();
    }
}
